package com.ecds.monitor.https;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import com.ecds.monitor.https.TrustSSL.TrustAnyHostnameVerifier;
import com.ecds.monitor.https.TrustSSL.TrustAnyTrustManager;

public class GetHtmlThread extends Thread{
	public String html;
	private String urlStr;

	public GetHtmlThread(String urlStr) {
		this.urlStr = urlStr;
	}

	@SuppressWarnings("null")
	public void run(){
		InputStream in = null;
		OutputStream out = null;
		try {
			SSLContext sc = null;
			try{
			sc = SSLContext.getInstance("SSL");
			sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
					new java.security.SecureRandom());
			}catch(Exception e){
				
			}
			URL console = new URL(urlStr);
			HttpsURLConnection conn = (HttpsURLConnection) console
					.openConnection();
			conn.setSSLSocketFactory(sc.getSocketFactory());
			conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
			conn.connect();
			InputStream is = conn.getInputStream();
			DataInputStream indata = new DataInputStream(is);
			String ret = "";
			while (ret != null) {
				ret = indata.readLine();
				if (ret != null && !ret.trim().equals("")) {
					html = html	+ new String(ret.getBytes("ISO-8859-1"), "UTF-8");
				}
			}
			conn.disconnect();
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (Exception e) {
			}
			try {
				out.close();
			} catch (Exception e) {
			}
		}
	}
}
